<template>
	<view class="XQCon">
		<u-navbar title="商品详情" :background="{ background: '#a0cfff' }" title-color="#FFFFFF"></u-navbar>
		<view class="u-m-15">
			<u-swiper :list="list" height="350" :title="false"></u-swiper>
		</view>
		<view class="u-m-15 u-p-30 XQmon">
			<view>
				<text style="font-size: 40rpx;color: #FF0000;">现价：￥{{shopSign.price}}</text>
				<text class="u-m-l-30">原价：￥{{shopSign.original}}</text>
			</view>
			<view class="flex-sbc u-m-t-15 u-m-b-15">
				<view class="left">
					<text>品质：{{shopSign.lity.listType}}成新</text>
				</view>
				<view class="right">
					<text>浏览：{{shopSign.shopLook}}</text>
				</view>
			</view>
			<view class="XQT">
				{{shopSign.title}}
			</view>
		</view>
		<view class="u-content u-m-15 u-p-30">
			<u-parse :html="shopSign.content"></u-parse>
		</view>

		<view class="u-m-15">
			<view class="comment" v-for="(res, index) in commentList" :key="res.id">
				<view class="left">
					<image :src="res.url" mode="aspectFill"></image>
				</view>
				<view class="right">
					<view class="top">
						<view class="name">{{ res.name }}</view>
						<view class="like" :class="{ highlight: res.isLike }">
							<!-- <view class="num">{{ res.likeNum }}</view> -->
							<!-- <u-icon v-if="!res.isLike" name="thumb-up" :size="30" color="#9a9a9a"
								@click="getLike(index)"></u-icon>
							<u-icon v-if="res.isLike" name="thumb-up-fill" :size="30" @click="getLike(index)"></u-icon> -->
						</view>
					</view>
					<view class="content">{{ res.contentText }}</view>
					<view class="reply-box" v-if="res.replyList.length >0">
						<view class="item" v-for="(item, index) in res.replyList" :key="item.index">
							<view class="username">{{ item.name }} <text class="u-m-l-15" style="color: red;">商家</text></view>
							<view class="text">{{ item.contentStr }}</view>
						</view>
						<view class="all-reply" @tap="toAllReply" v-if="res.replyList != undefined">
							共{{ res.allReply }}条回复
							<u-icon class="more" name="arrow-right" :size="26"></u-icon>
						</view>
					</view>
					<view class="bottom">
						{{ res.date }}
					</view>
				</view>
			</view>
		</view>
		
		<view style="height: 120rpx;"></view>

		<view class="navigation">
			<view class="left">
				<view class="item">
					<u-icon name="server-fill" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">客服</view>
				</view>
				<view class="item" @click="reqClick(shopSign)">
					<u-icon name="home" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">举报</view>
				</view>
				<view class="item car">
					<u-badge class="car-num" :count="collectNum" type="error" :offset="[-3, -6]"></u-badge>
					<u-icon name="shopping-cart" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">购物车</view>
				</view>
			</view>
			<view class="right"
				v-if="shopSign.shopNum > shopSign.shopBuy && shopSign.shopSold == false && shopSign.deleteType == false">
				<view class="cart btn u-line-1" @click="collectClick()">加入购物车</view>
				<view class="buy btn u-line-1" @click="suShow = true">立即购买</view>
			</view>
			<view class="right" v-else>
				<view class="buy btn u-line-1">商品已买完、修改或下架</view>
			</view>
		</view>

		<u-popup v-model="suShow" mode="bottom">
			<view class="u-m-40">
				<view class="u-m-30" style="text-align: center;">填写信息</view>
				<u-form :model="form" ref="uForm" label-width="150">
					<u-form-item label="姓名">
						<u-input v-model="form.username" />
					</u-form-item>
					<u-form-item label="电话">
						<u-input v-model="form.phone" @click="typeClick" />
					</u-form-item>
					<u-form-item label="地址">
						<u-input type="textarea" v-model="form.address" />
					</u-form-item>
					<u-form-item label="时间">
						<u-input v-model="form.time" />
					</u-form-item>
				</u-form>
				<view class="u-m-t-30">
					<u-button type="error" @click="addOrder()">确定</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import helper from '../../common/helper.js'
	import time from '../../common/time.js'
	export default {
		data() {
			return {
				shopId: '',
				shopSign: {
					lity: {
						listType: ''
					}
				},
				collectNum: 0,
				list: [],
				suShow: false,
				form: {
					username: '',
					phone: '',
					address: '',
					time: '明天上午12点'
				},
				commentList: [
					// 	{
					// 	id: 1,
					// 	name: '叶轻眉',
					// 	date: '12-25 18:58',
					// 	contentText: '我不信伊朗会没有后续反应，美国肯定会为今天的事情付出代价的',
					// 	url: 'https://cdn.uviewui.com/uview/template/SmilingDog.jpg',
					// 	allReply: 12,
					// 	likeNum: 33,
					// 	isLike: false,
					// 	replyList: [{
					// 			name: 'uview',
					// 			contentStr: 'uview是基于uniapp的一个UI框架，代码优美简洁，宇宙超级无敌彩虹旋转好用，用它！'
					// 		},
					// 		{
					// 			name: '粘粘',
					// 			contentStr: '今天吃什么，明天吃什么，晚上吃什么，我只是一只小猫咪为什么要烦恼这么多'
					// 		}
					// 	]
					// },
				]
			}
		},
		onLoad: function(options) {
			let that = this
			// console.log(options)
			if (options.id != "") {
				that.shopId = options.id
				that.addShopLook()
				that.lookCollect()
				that.getComment()
				// that.getShop()
			}
		},
		created() {

		},
		methods: {
			//商品信息
			getShop() {
				let that = this
				let data = {
					id: that.shopId
				}
				that.getRequest(helper.getShopSign, data).then(res => {
					if (res.data.flag == "200") {
						let serve = res.data
						if (serve.shopSing.shopUrl.indexOf("<<tupian>>") > 0) {
							serve.shopSing.shopUrl = serve.shopSing.shopUrl.split("<<tupian>>")
							serve.shopSing.shopUrl.forEach(item => {
								let obj = {
									shopId: item.id,
									image: item
								}
								that.list.push(obj)
							})
						} else {
							let obj = {
								image: serve.shopSing.shopUrl
							}
							that.list.push(obj)
						}
						that.shopSign = serve.shopSing
					}
				})
			},
			//加入购物车
			collectClick() {
				let that = this
				let user = uni.getStorageSync("user")
				let data = {
					userId: user.id,
					shopId: that.shopId,
					type: 1
				}
				that.postRequest(helper.addCollect, data).then(res => {
					if (res.data.flag == "200") {
						that.lookCollect()
						uni.showToast({
							title: '成功加入购物车',
							icon: 'none',
							duration: 2000
						})
					} else {
						uni.showToast({
							title: '加入购物车失败',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			//购物车数量
			lookCollect() {
				let that = this
				let user = uni.getStorageSync("user")
				let data = {
					userId: user.id,
					shopId: that.shopId,
					type: 1
				}
				that.postRequest(helper.lookCollect, data).then(res => {
					if (res.data.flag == "200") {
						let serve = res.data
						that.collectNum = serve.collectNum
					}
				})
				
			},
			//购买商品
			addOrder() {
				let that = this
				let user = uni.getStorageSync("user")
				let data = {
					userId: user.id,
					shopId: that.shopId,
					name: that.form.username,
					phone: that.form.phone,
					address: that.form.address,
					time: that.form.time,
					orderType: 1,
					sellerId: that.shopSign.userId
				}
				that.postRequest(helper.addOrder, data).then(res => {
					if (res.data.flag == "200") {
						uni.navigateTo({
							url: './shopSucces?title=' + that.shopSign.title + '&price=' + that.shopSign
								.price + '&time=' + that.form.time + '&address=' + that.form.address
						})
					}
				})
			},
			//添加商品浏览数
			addShopLook() {
				let that = this
				let data = {
					id: that.shopId,
				}
				// console.log(data)
				that.postRequest(helper.addShopLook, data).then(res => {
					if (res.data.flag == "200") {
						that.getShop()
						// that.getComment()
					}
				})
			},
			getComment() {
				let that = this
				let data = {
					shopId: that.shopId,
				}
				that.postRequest(helper.getComment, data).then(res => {
					if (res.data.flag == "200") {
						let serve = res.data.commentList
						serve.forEach((item, index) => {
							if (!item.commentId) {
								item.creatTime = that.formatDateTime(item.creatTime)
								let obj = {
									id: item.id,
									name: item.user.username,
									date: item.creatTime,
									contentText: item.content,
									url: item.user.userUrl,
									allReply: 0,
									likeNum: 0,
									isLike: false,
									replyList: []
								}
								that.commentList.push(obj)
							}
						})
						serve.forEach((item, index) => {
							if (item.commentId) {
								item.creatTime = that.formatDateTime(item.creatTime)
								
								that.commentList.forEach((it,index1) => {
									if(it.id == item.commentId){
										let obj = {
											name: item.user.username,
											contentStr: item.content
										}
										that.commentList[index1].replyList.push(obj)
										that.commentList[index1].allReply ++
									}
								})
							}
						})
					}
				})
			},
			reqClick() {
				let that = this
				uni.navigateTo({
					url: './shopReport?shopId=' + that.shopSign.id + "&title=" + that.shopSign.title
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: rgb(240, 240, 240);
	}
</style>
<style lang="scss" scoped>
	.XQCon {
		.XQmon {
			background-color: #FFFFFF;
			border-radius: 25rpx;

			.XQT {
				font-size: 42rpx;
			}

		}

		.u-content {
			background-color: #FFFFFF;
			border-radius: 25rpx;
		}

		.navigation {
			display: flex;
			// margin-top: 100rpx;
			border: solid 2rpx #f2f2f2;
			background-color: #ffffff;
			padding: 16rpx 0;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;

			.left {
				display: flex;
				font-size: 20rpx;

				.item {
					margin: 0 30rpx;

					&.car {
						text-align: center;
						position: relative;

						.car-num {
							position: absolute;
							top: -10rpx;
							right: -10rpx;
						}
					}
				}
			}

			.right {
				display: flex;
				font-size: 28rpx;
				align-items: center;

				.btn {
					line-height: 66rpx;
					padding: 0 30rpx;
					border-radius: 36rpx;
					color: #ffffff;
				}

				.cart {
					background-color: #ed3f14;
					margin-right: 30rpx;
				}

				.buy {
					background-color: #ff7900;
				}
			}
		}
	}



	.comment {
		background-color: #FFFFFF;
		border-radius: 20rpx;
		display: flex;
		padding: 30rpx;

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.name {
					color: #5677fc;
				}

				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;

					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}

				.highlight {
					color: #5677fc;

					.num {
						color: #5677fc;
					}
				}
			}

			.content {
				margin-bottom: 10rpx;
			}

			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;

				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;

					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}

				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;

					.more {
						margin-left: 6rpx;
					}
				}
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;

				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}
</style>
